package com.csyf.manager.dao;

import com.csyf.manager.entity.SysTrade;

public class OrderSqlProvider {

    public String sumTradeFee(SysTrade vo) {

        StringBuffer sql = new StringBuffer("SELECT IFNULL(SUM(IFNULL(total_fee,0)),0) AS totalFee, IFNULL(SUM(IFNULL(profit,0)),0) AS profit FROM sys_trade WHERE 1=1");
        if(null != vo.getAgentId()){
            sql.append(" AND agent_id = #{agentId}");
        }
        if(null != vo.getMchId() && vo.getMchId() != ""){
            sql.append(" AND mch_id = #{mchId}");
        }
        if(null != vo.getSrcMchId() && vo.getSrcMchId() != ""){
            sql.append(" AND src_mch_id = #{srcMchId}");
        }
        if(null != vo.getOutTradeNo() && vo.getOutTradeNo() != ""){
            sql.append(" AND out_trade_no = #{outTradeNo}");
        }
        if(null != vo.getSrcOrderNo() && vo.getSrcOrderNo() != ""){
            sql.append(" AND src_order_no = #{srcOrderNo}");
        }
        if(null != vo.getServiceType() && vo.getServiceType() != ""){
            sql.append(" AND service_type = #{serviceType}");
        }
        if(null != vo.getTradeStatus() && vo.getTradeStatus() != ""){
            sql.append(" AND trade_status = #{tradeStatus}");
        }
        if(null != vo.getCreateTimeString() && vo.getCreateTimeString() != ""
                && null != vo.getEndTimeString() && vo.getEndTimeString() != "") {
            vo.setCreateTimeString(vo.getCreateTimeString()+" 00:00:00");
            vo.setEndTimeString(vo.getEndTimeString() + " 23:59:59");
            sql.append(" AND create_time BETWEEN CONCAT(#{createTimeString},' 00:00:00') AND CONCAT(#{endTimeString},' 23:59:59')");
        }
        return sql.toString();
    }
}
